package com.guigu.health.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.health.dao.MemberMapper;
import com.guigu.health.dao.RoleMapper;
import com.guigu.health.dao.UserMapper;
import com.guigu.health.dao.UserRolePermissionMapper;
import com.guigu.health.pojo.Member;
import com.guigu.health.pojo.Role;
import com.guigu.health.pojo.User;
import com.guigu.health.pojo.UserExample;
import com.guigu.health.service.UserService;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

/**
 * @ClassName:UserServiceImpl
 * @Description:用户业务实现类
 * @Author:longtao
 * @Date:2021/3/26
 * @Version:1.0
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    /**
     * 用户dao层
     */
    UserMapper userMapper;
    @Autowired
    @Qualifier(value = "userMapper")
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    private UserRolePermissionMapper userRolePermissionMapper;
    @Autowired
    @Qualifier(value = "userRolePermissionMapper")
    public void setUserRolePermissionMapper(UserRolePermissionMapper userRolePermissionMapper) {
        this.userRolePermissionMapper = userRolePermissionMapper;
    }

    /**
     * 会员dao层
     */
    MemberMapper memberMapper;
    @Autowired
    @Qualifier(value = "memberMapper")
    public void setMemberMapper(MemberMapper memberMapper) {
        this.memberMapper = memberMapper;
    }

    private RoleMapper roleMapper;
    @Autowired
    @Qualifier(value = "roleMapper")
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    @Override
    public boolean addUser(User user) {
        /**
         * 使用随机数再次进行加密
         */
        long l = RandomUtils.nextLong();
        String salt=String.valueOf(l);
        /**
         * 参数:
         * 1.原始密码
         * 2.随机盐
         * 3.哈希散列
         */
        Md5Hash md5Hash=new Md5Hash(user.getPassword(),salt,1024);
        String password=String.valueOf(md5Hash);
        user.setPassword(password);
        user.setSalt(salt);
        boolean flag=false;
        int i = userMapper.insert(user);
        if (i>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public User loginUser(String account) {
        return userMapper.loginUser(account);
    }

    @Override
    public String getSalt(String account) {
        return userMapper.getSalt(account);
    }

    @Override
    public boolean addMember(Member member) {
        boolean flag=false;
        int i = memberMapper.insert(member);
        if (i>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public boolean updatePassword(String oldPassword,String newPassword,String account) {

        boolean flag=false;
        long l = RandomUtils.nextLong();
        String salt=String.valueOf(l);
        Md5Hash md5Hash=new Md5Hash(newPassword,salt,1024);
        /**
         * 获取之前的随机盐来判断密码是否对
         */
        String salt2=userMapper.getSalt(account);
        Md5Hash md5Hash1=new Md5Hash(oldPassword,salt2,1024);
        int i = userMapper.updatePassowrd(String.valueOf(md5Hash1),String.valueOf(md5Hash),account,salt);
        if (i>0){
         flag=true;
        }
        return flag;
    }

    @Override
    public boolean updateUser(User user, UserExample example) {
        boolean flag=false;
        int i = userMapper.updateByExampleSelective(user, example);
        if (i>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public List<User> queryAllUser(UserExample example) {
        List<User> list = userMapper.selectByExample(example);
        return list;
    }

    /**
     * 根据用户id删除用户和角色之间的关系
     *
     * @param userId
     */
    @Override
    public void deleteUserRoleKey(int userId) {
        userRolePermissionMapper.deleteUserRoleKey(userId);
    }

    /**
     * 添加用户和角色之间的关系
     *
     * @param userId
     * @param roleIds
     */
    @Override
    public void addUserRoleKey(int userId, int[] roleIds) {
        if (roleIds!=null){
            for (int id : roleIds) {
                userRolePermissionMapper.addUserRoleKey(userId,id);
            }
        }
    }

    /**
     * 根据用户id删除用户
     *
     * @param userId
     * @return
     */
    @Override
    @Transactional
    public boolean deleteUser(int userId) {
        boolean flag=false;
        /**
         * 删除用户和角色之间的关系,在进行删除用户
         */
        try {
            deleteUserRoleKey(userId);
        }catch (Exception e){
            return false;
        }
        int i = userRolePermissionMapper.deleteByPrimaryKey(userId);
        if (i>0){
            flag=true;
        }
        return flag;
    }

    /**
     * 模糊查询并分页
     *
     * @param example
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<User> queryAllUser(UserExample example, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userMapper.selectByExample(example);
        if (null!=users){
            PageInfo<User> pageInfo=new PageInfo<User>(users);
            return pageInfo;
        }
        return null;
    }

    /**
     * 用户修改以及修改用户和角色之间的关系
     *
     * @param example
     * @param user
     * @param roleIDs
     */
    @Override
    @Transactional
    public void updateUserRoleKeyAndUser(UserExample example, User user, int[] roleIDs) {
        /**
         * 删除用户和角色之间的关系
         */
        deleteUserRoleKey(user.getId());
        /**
         * 添加角色和用户之间的关系
         */
        addUserRoleKey(user.getId(),roleIDs);
        /**
         * 修改用户
         */
        userRolePermissionMapper.updateByExampleSelective(user,example);
    }

    /**
     * 根据用户id查询出对应的所有角色
     *
     * @param userIds
     * @return
     */
    @Override
    public List<Integer> queryByRoleIds(int userIds) {
        List<Integer> list = userRolePermissionMapper.queryRoleIds(userIds);
        if (null==list){
            return null;
        }
       return list;
    }

    /**
     * 查询出角色
     *
     * @return
     */
    @Override
    public List<Role> queryAllRole() {
        return roleMapper.selectByExample(null);
    }
}
